Skip to content

[clang-driver] don't always set defaultlib=libcmt and defaultlib=oldnames #149400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

AlexRouSg
Copy link

when /MD or -fms-runtime-lib=dll or the debug equivalent is set this results in conflicting runtimes

fixes #129881

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jul 17, 2025
@AlexRouSg AlexRouSg changed the title [clang-driver] don't always set defaultlib=libcmd and defaultlib=oldnames [clang-driver] don't always set defaultlib=libcmt and defaultlib=oldnames Jul 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 17, 2025

@llvm/pr-subscribers-clang

Author: Chotepud Teo (AlexRouSg)

Changes

when /MD or -fms-runtime-lib=dll or the debug equivalent is set this results in conflicting runtimes

fixes #129881


Full diff: https://github.com/llvm/llvm-project/pull/149400.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/MSVC.cpp (+6-2)
  • (modified) clang/test/Driver/cl-runtime-flags.c (+4)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 7d31eea603087..36edeb7163e8c 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -85,8 +85,12 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
       CmdArgs.push_back(Args.MakeArgString("--dependent-lib=amath"));
   }
 
-  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
-      !C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
+  StringRef msrtlib = Args.getLastArgValue(options::OPT_fms_runtime_lib_EQ, "");
+  bool dllmsrt = msrtlib.equals_insensitive("dll") ||
+                 msrtlib.equals_insensitive("dll_dbg");
+  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
+                   options::OPT__SLASH_MD, options::OPT__SLASH_MDd) &&
+      !dllmsrt && !C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
     CmdArgs.push_back("-defaultlib:libcmt");
     CmdArgs.push_back("-defaultlib:oldnames");
   }
diff --git a/clang/test/Driver/cl-runtime-flags.c b/clang/test/Driver/cl-runtime-flags.c
index 2a6151e08c046..7fe4f543ac806 100644
--- a/clang/test/Driver/cl-runtime-flags.c
+++ b/clang/test/Driver/cl-runtime-flags.c
@@ -38,6 +38,8 @@
 // CHECK-MD: "-D_DLL"
 // CHECK-MD: "--dependent-lib=msvcrt"
 // CHECK-MD: "--dependent-lib=oldnames"
+// CHECK-MD-NOT: "-defaultlib:libcmt"
+// CHECK-MD-NOT: "-defaultlib:oldnames"
 
 // RUN: %clang_cl -### /MDd -- %s 2>&1 | FileCheck -check-prefix=CHECK-MDd %s
 // RUN: %clang -### --target=x86_64-windows-msvc -fms-runtime-lib=dll_dbg -- \
@@ -47,6 +49,8 @@
 // CHECK-MDd: "-D_DLL"
 // CHECK-MDd: "--dependent-lib=msvcrtd"
 // CHECK-MDd: "--dependent-lib=oldnames"
+// CHECK-MDd-NOT: "-defaultlib:libcmt"
+// CHECK-MDd-NOT: "-defaultlib:oldnames"
 
 // RUN: %clang_cl -### /LD -- %s 2>&1 | FileCheck -check-prefix=CHECK-LD %s
 // RUN: %clang_cl -### /LD /MT -- %s 2>&1 | FileCheck -check-prefix=CHECK-LD %s

@llvmbot
Copy link
Member

llvmbot commented Jul 17, 2025

@llvm/pr-subscribers-clang-driver

Author: Chotepud Teo (AlexRouSg)

Changes

when /MD or -fms-runtime-lib=dll or the debug equivalent is set this results in conflicting runtimes

fixes #129881


Full diff: https://github.com/llvm/llvm-project/pull/149400.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/MSVC.cpp (+6-2)
  • (modified) clang/test/Driver/cl-runtime-flags.c (+4)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 7d31eea603087..36edeb7163e8c 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -85,8 +85,12 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
       CmdArgs.push_back(Args.MakeArgString("--dependent-lib=amath"));
   }
 
-  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
-      !C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
+  StringRef msrtlib = Args.getLastArgValue(options::OPT_fms_runtime_lib_EQ, "");
+  bool dllmsrt = msrtlib.equals_insensitive("dll") ||
+                 msrtlib.equals_insensitive("dll_dbg");
+  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
+                   options::OPT__SLASH_MD, options::OPT__SLASH_MDd) &&
+      !dllmsrt && !C.getDriver().IsCLMode() && !C.getDriver().IsFlangMode()) {
     CmdArgs.push_back("-defaultlib:libcmt");
     CmdArgs.push_back("-defaultlib:oldnames");
   }
diff --git a/clang/test/Driver/cl-runtime-flags.c b/clang/test/Driver/cl-runtime-flags.c
index 2a6151e08c046..7fe4f543ac806 100644
--- a/clang/test/Driver/cl-runtime-flags.c
+++ b/clang/test/Driver/cl-runtime-flags.c
@@ -38,6 +38,8 @@
 // CHECK-MD: "-D_DLL"
 // CHECK-MD: "--dependent-lib=msvcrt"
 // CHECK-MD: "--dependent-lib=oldnames"
+// CHECK-MD-NOT: "-defaultlib:libcmt"
+// CHECK-MD-NOT: "-defaultlib:oldnames"
 
 // RUN: %clang_cl -### /MDd -- %s 2>&1 | FileCheck -check-prefix=CHECK-MDd %s
 // RUN: %clang -### --target=x86_64-windows-msvc -fms-runtime-lib=dll_dbg -- \
@@ -47,6 +49,8 @@
 // CHECK-MDd: "-D_DLL"
 // CHECK-MDd: "--dependent-lib=msvcrtd"
 // CHECK-MDd: "--dependent-lib=oldnames"
+// CHECK-MDd-NOT: "-defaultlib:libcmt"
+// CHECK-MDd-NOT: "-defaultlib:oldnames"
 
 // RUN: %clang_cl -### /LD -- %s 2>&1 | FileCheck -check-prefix=CHECK-LD %s
 // RUN: %clang_cl -### /LD /MT -- %s 2>&1 | FileCheck -check-prefix=CHECK-LD %s

…ames

when /MD or -fms-runtime-lib=dll or the debug equivalent is set this results in conflicting runtimes

fixes llvm#129881
@lyj-514328
Copy link

What is the purpose of explicitly adding 'defaultlib' here? When compiling with MSVC header files, the generated object files should already contain the appropriate directives indicating the defaultlib.
Additionally, using the /Bz option with cl.exe also shows that no 'defaultlib' has been added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang,windows: why doesn't -fms-runtime-lib=dll implicitly pass -Wl,/NODEFAULTLIB:libcmt?
3 participants